package com.hhd.pdf.components;


import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfDiv;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class TitleComponent extends AbstractPdfComponent {

    @Override
    public void parseComponent(Element element, GeneratorContext context) throws Exception {
        List<com.itextpdf.text.Element> elements = parseSimpleNode(element, context);
        Font font = PdfUtils.createFont(16, context.getGeneratorParameters().getColor(), Font.BOLD);
        Paragraph p = PdfUtils.createParagraph(font, Paragraph.ALIGN_CENTER);

        for (com.itextpdf.text.Element element1 : elements) {
            if (PdfUtils.isPhrase(element1)) {
                p.add(PdfUtils.changeFont(PdfUtils.toPhrase(element1), font));
            } else {
                p.add(element1);
            }
        }

        PdfDiv div = PdfUtils.createDiv(p, 10, 10);
        context.getDocument().add(div);
    }

    @Override
    public String getName() {
        return "title";
    }
}
